'use strict';

const Controller = require('egg').Controller;
// 用户角色相关请求响应定义
const adminRolesReqAndResp = require('../../entitys/reqAndResp/admin/adminRoles');

/**
 * 创建者: rliang
 * 创建时间: 2019年08月23日 10:44:59
 * 功能描述: 角色相关操作
 */
class AdminRolesController extends Controller {

  // 分页查询角色
  async queryAdminRolesList() {
    const { ctx, service } = this;
    // 定义返回数据 data 中的数据结构
    const response = adminRolesReqAndResp.queryAdminRolesList.response();
    // 检查请求参数并获得请求对象
    const request = ctx.request.getAndValidate_QueryAdminRolesList();
    // 从这里开始写业务逻辑
    const adminRolesList = await service.admin.adminRoles.queryAdminRolesList(request.name, request.pageSize, request.pageIndex);
    // 查询数据总条数
    const dataCount = await service.admin.adminRoles.queryAdminRolesListCount(request.name);
    // 添加操作日志
    ctx.writeSysOperationLogs('查询角色列表', ctx.getSystemLogType('QUERY'), { tableName: '角色表' });
    if (adminRolesList) {
      response.pageCount = Math.ceil(dataCount / request.pageSize);
      response.dataCount = dataCount;
      response.adminRolesList = adminRolesList;
      ctx.response.success(response);
      return;
    }
    // 未查到结果时
    response.pageCount = 0;
    response.dataCount = 0;
    response.rolesList = [];
    ctx.response.success(response);
    return;
  }

  // 添加角色信息
  async addAdminRole() {
    const { ctx, service } = this;
    const request = ctx.request.getAndValidate_addAdminRoleRequest();
    const result = await service.admin.adminRoles.addAdminRole(request);

    if (result) {
      ctx.response.success();
      // 添加操作日志
      ctx.writeSysOperationLogs('添加角色信息', ctx.getSystemLogType('CREATE'), { tableName: '角色表' });
      return;
    }
    ctx.response.fail();
  }

  // 修改角色信息
  async updateAdminRole() {
    const { ctx, service } = this;
    const request = ctx.request.getAndValidate_UpdateAdminRoleRequest();
    const result = await service.admin.adminRoles.updateAdminRole(request);

    if (result) {
      ctx.response.success();
      // 添加操作日志
      ctx.writeSysOperationLogs('修改角色信息', ctx.getSystemLogType('UPDATE'), { tableName: '角色表' });
      return;
    }

    ctx.response.fail();
  }
  
  async setRoleAutho() {
    const { ctx, service } = this;
    const request = ctx.request.getAndValidate_SetRoleAuthoRequest();
    // 设置权限
    const isSuccess = await service.admin.adminRoles.setRoleAutho(request.roleId, request.menus);
    if (isSuccess) {
      ctx.response.success();
      // 添加操作日志
      ctx.writeSysOperationLogs('为角色分配权限', ctx.getSystemLogType('UPDATE'), { tableName: '角色权限关系表' });
      return;
    }
    return ctx.response.fail();
  }

  /**
   * 获取角色拥有的菜单列表（功能级别）
   */
  async getRoleMenuUserRights() {
    const { ctx, service } = this;
    const request = ctx.request.getAndValidate_getRoleMenuUserRights();
    // 获取角色拥有的菜单列表（功能级别）
    const resMenus = await service.admin.sysMenus.queryRoleMenuUserRights(request.roleId);
    ctx.response.success(resMenus);
  }

  async getRoleAutho() {
    const { ctx, service } = this;
    const request = ctx.request.getAndValidate_GetRoleAuthoRequest();

    const resMenuIds = await service.admin.sysMenus.queryMenuIdByRoleId(request.roleId);
    
    ctx.response.success({ menuIds: resMenuIds });
    // 添加操作日志
    ctx.writeSysOperationLogs('查询角色权限', ctx.getSystemLogType('QUERY'), { tableName: '角色权限关系表' });
  }

  async deleteAdminRole() {
    const { ctx, service } = this;
    const request = ctx.request.getAndValidate_DeleteAdminRoleRequest();
    const result = await service.admin.adminRoles.queryAdminRole(request.id);
    if (result && result.length > 0) {
      ctx.response.fail('请先删除用户角色关联的数据!');
      return;
    }
    const data = await service.admin.adminRoles.deleteAdminRole(request);
    if (data) {
      ctx.response.success();
      // 添加操作日志
      ctx.writeSysOperationLogs('删除角色', ctx.getSystemLogType('DELETE'), { tableName: '角色表' });
      return;
    }
    ctx.response.fail('删除失败');
  }
}

module.exports = AdminRolesController;
